﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ContractManager.Common.Service;
using DevExpress.XtraEditors;

namespace ContractManager.Common.FormBase
{
    public partial class XtraFormBase : XtraForm
    {
        #region "Define Variable"
        public int AsSysMyCoYearId { get; set; }
        public int AsSysBillStyleId { get; set; }
        public int AsSysBillStyleId2 { get; set; }
        public int AsPerPersonId { get; set; }
        
        public int FormSysBillStyleIdExport { get; set; }//Chứng tù nhập, xuất kho từ hoá đơn
        public string FormXtraLayoutTemplate { get; set; }//Mẫu giao diện nhập
        public string FormXtraGridLayoutTemplate { get; set; }//Mẫu Layout grid
        public string FormReportLayoutTemplate { get; set; }//Mẫu Report
        public string FormDetailVatGridLayout { get; set; }//Mẫu thu chi trong trường hợp có thuế
        public string FormXtraGridSearchLayout { get; set; }//Mẫu layout Grid chi tiết trong form chứ thông tin chung
        public string StrStockDynamic { get; set; }//Điều kiện lọc kho
        public string StrReasonDynamic { get; set; }//Điều kiện lọc lý do nhập, xuất kho
        public string StrVatBillStyleDynamic { get; set; } //Điều kiện lọc loại hoá đơn GTGT
        public bool FormIsPrintAfterSave { get; set; }
        public bool FormIsNotValidate { get; set; }
        public int FormAccountDefault { get; set; }
        public int FormNo { get; set; }
        public int VoucherCate { get; set; }
        public bool FormIsSurvival { get; set; }//Theo dõi lượng tồn hàng hoá
        public bool FormIsGetPriceByName { get; set; }//Sử dụng giá đích danh từ phiếu nhập
        public bool IsPrintAfterSave { get; set; }
        public string ReportName { get; set; }
        public string StoreProcedureName { get; set; }
        
        public string Form_TableName { get; set; }
        public string Form_TableKeyField { get; set; }
        public string Form_Detail_AssemblyName { get; set; }
        public string Form_Detail_FormClass { get; set; }
        public string Form_Detail_SqlGetByMaster { get; set; }
        public string Form_Detail_SqlDeleteByMaster { get; set; }
        public string Form_SourceSql { get; set; }
        public bool Form_IsBusiness { get; set; }

        public bool SaveFormMaster { get; set; }


        public bool AllowAdd;
        public bool AllowDelete;
        public bool AllowEdit;
        public bool AllowAccess;
        public bool AllowPrint;
        public bool AllowExport;
        public bool AllowImport;

        public bool AddMode { get; set; }
        public bool EditMode { get; set; }
        public bool DeleteMode { get; set; }

        public List<DateTime> LstLockDate { get; set; }

        public string FieldNameId { get; set; }
        public string TableName { get; set; }
        public string StrCaption { get; set; }
        public int Row { get; set; }

        public GridCheckMarksSelection GcmSelection { get; set; }
        public RefreshHelper RefHelper;
        #endregion

        #region "Construction"
        public XtraFormBase()
        {
            InitializeComponent();
            if (!String.IsNullOrEmpty(StrCaption))
            {
                this.Text = StrCaption;
            }
        }
        #endregion

        #region "Dispose Form"
        ~XtraFormBase()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
        #endregion

        #region "Public Method"
        public void ResetControls(Control cur)
        {
            foreach (Control c in cur.Controls)
            {
                var textEdit = c as TextEdit;
                var dateEdit = c as DateEdit;
                var memoEdit = c as MemoEdit;
                var calcEdit = c as CalcEdit;
                var lookUpEdit = c as LookUpEdit;
                var searchLookUpEdit = c as SearchLookUpEdit;

                if (textEdit != null)
                    ((TextEdit)c).EditValue = null;
                if (dateEdit != null)
                    ((DateEdit)c).EditValue = null;
                if (memoEdit != null)
                    ((MemoEdit)c).EditValue = null;
                if (calcEdit != null)
                    ((CalcEdit)c).EditValue = 0;
                if (lookUpEdit != null)
                    ((LookUpEdit)c).EditValue = null;
                if (searchLookUpEdit != null)
                    ((SearchLookUpEdit)c).EditValue = null;
                
                if (c.HasChildren)
                    ResetControls(c);
            }
        }

        public void ResetControls()
        {
            foreach (Control c in Controls)
            {
                var textEdit = c as TextEdit;
                var dateEdit = c as DateEdit;
                var memoEdit = c as MemoEdit;
                var calcEdit = c as CalcEdit;
                var lookUpEdit = c as LookUpEdit;
                var searchLookUpEdit = c as SearchLookUpEdit;
                var gridLookUpEdit = c as GridLookUpEdit;

                if (textEdit != null)
                    ((TextEdit)c).EditValue = null;
                if (dateEdit != null)
                    ((DateEdit)c).EditValue = null;
                if (memoEdit != null)
                    ((MemoEdit)c).EditValue = null;
                if (calcEdit != null)
                    ((CalcEdit)c).EditValue = 0;
                if (lookUpEdit != null)
                    ((LookUpEdit)c).EditValue = null;
                if (searchLookUpEdit != null)
                    ((SearchLookUpEdit)c).EditValue = null;
                if (gridLookUpEdit != null)
                    ((GridLookUpEdit)c).EditValue = null;

                if (c.HasChildren)
                    ResetControls(c);
            }
        }

        public void DisposeControls()
        {
            foreach (Control c in Controls)
            {
                c.Dispose();
            }
        }
        #endregion

        #region "Private Method"
        
        #endregion

        #region "Form Event"
        private void XtraFormBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (Disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(Disposing);


            //this.DisposeControls();
            Dispose();
            //this.Close();
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
        #endregion
    }
}